package design.state;

// 状态模式
// spring状态机，处理订单
// https://juejin.cn/post/6844904098676867086#heading-7
// 实际上很少使用Spring Status Machine，都用数据库来判断字段状态即可

// 工程模板
// https://bugstack.cn/md/develop/design-pattern/2020-07-02-%E9%87%8D%E5%AD%A6%20Java%20%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%E3%80%8A%E5%AE%9E%E6%88%98%E7%8A%B6%E6%80%81%E6%A8%A1%E5%BC%8F%E3%80%8B.html

// 订单流转
// https://mp.weixin.qq.com/s/FVFyMwSBQeG70Rw9_91wTw

// 状态模式3部曲:
// 定义一个状态接口，并设置可以改变状态的接口传入Context
// 定义Context存储状态，并设置构造方法与Set方法
// 具体状态重写接口方法，改变状态

interface State {
    void handle(Context context);
}

class ConcreteStateA implements State {

    @Override
    public void handle(Context context) {
        System.out.println("Handling State A");
        // A -> B
        context.setState(new ConcreteStateB());
    }
}

class ConcreteStateB implements State {
    @Override
    public void handle(Context context) {
        System.out.println("Handling State B");
        // B -> A
        context.setState(new ConcreteStateA());
    }
}


class Context {
    private State state;

    public Context(State state) {
        this.state = state;
    }

    public void setState(State state) {
        this.state = state;
    }

    public void request() {
        state.handle(this);
    }
}


public class StatePatternDemo {
    public static void main(String[] args) {
        Context context = new Context(new ConcreteStateA());
        context.request();
        context.request();
        context.request();
    }
}
